/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once
#include "record_record.h"
#include <mutex>
#include <unordered_map>
#include <sstream>
#include "utils_log.h"
#include "record_recorder.h"
#include "nlohmann/json.hpp"
#include "storage_manager.h"
namespace El {
namespace Record {

class CRecordImpl : public IRecord {
public:
    CRecordImpl();

    ~CRecordImpl() override;

    bool Start() override;

    void Stop() override;

    int32_t StartManual(uint32_t ch) override;

    void StopManual(uint32_t ch) override;

    bool GetRecordStatus(uint32_t ch) override;

private:
    bool ResetConfig(const nlohmann::json &oldConfig, const nlohmann::json &newConfig);
    void NotifyStorageEvent(const std::shared_ptr<Storage::IStorageDevice> &device, Storage::DeviceStatus eventType);
    bool StartRecord();
    void StopRecord();

private:
    std::mutex mutex_;
    std::unordered_map<int32_t, RecorderPtr> recorders_;
    std::string storage_root_;
    nlohmann::json recordConfig_;
    nlohmann::json storageConfig_;
    uint32_t registerHandle_{std::numeric_limits<uint32_t>::max()};
    int32_t registerStorageHandle_{std::numeric_limits<int32_t>::max()};
    int32_t registerRcordHandle_{std::numeric_limits<int32_t>::max()};

    bool isStart_{false};
    bool isStartRecord_{false};
    Storage::DeviceStatus storageEventType_{Storage::DeviceStatus::ERR_NOT_EXIST};
};

} // namespace Record
} // namespace El
